package com.papper.common.utils.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class StringTool {
    public static final String        COMMA            = ",";
    public static final String        EMPTY            = "";
    public static final String        SINGLE_QUOTATION = "'";
    public static final DecimalFormat df               = new DecimalFormat("0.00");
    public static       char[]        hexDigits        = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * 字符比较
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean equals(String a, String b) {
        if (a == null) {
            return b == null;
        }
        return a.equals(b);
    }

    public static boolean equalsIgnoreCase(String a, String b) {
        if (a == null) {
            return b == null;
        }
        return a.equalsIgnoreCase(b);
    }

    /**
     * 字符串转LIST<String>
     */
    public final static List<String> stringToListString(String inStr, String split) {
        if (null == inStr || EMPTY.equals(inStr)) {
            return null;
        }

        List<String> rtn  = new ArrayList<String>();
        String[]     arry = inStr.split(split);
        for (String s : arry) {
            rtn.add(s);
        }
        return rtn;
    }

    /**
     * LIST<String>转字符串
     */
    public final static String ListStringToString(List<String> lst, String addStr) {
        if (null == lst || lst.isEmpty()) {
            return null;
        }

        String rtn = "";
        for (String l : lst) {
            rtn = rtn + addStr + l;
        }
        return isEmpty(rtn) ? rtn : rtn.substring(1);
    }

    /**
     * 字符串转LIST<Long>
     */
    public final static List<Long> stringToListLong(String inStr) {
        if (null == inStr || EMPTY.equals(inStr)) {
            return null;
        }

        List<Long> rtn  = new ArrayList<Long>();
        Long       l;
        String[]   arry = inStr.split(COMMA);
        for (String s : arry) {
            l = parseLong(s, null);
            if (l != null) {
                rtn.add(l);
            }
        }
        return rtn;
    }

    /**
     * 字符串转LIST<Integer>
     *
     * @param inStr
     * @return
     */
    public final static List<Integer> stringToListInteger(String inStr) {
        if (null == inStr || EMPTY.equals(inStr)) {
            return null;
        }

        List<Integer> rtn  = new ArrayList<Integer>();
        Integer       l;
        String[]      arry = inStr.split(COMMA);
        for (String s : arry) {
            l = parseInteger(s, null);
            if (l != null) {
                rtn.add(l);
            }
        }
        return rtn;
    }

    /**
     * 字符串转LIST<Long>
     */
    public final static String ListIntegerToString(List<Integer> lst) {
        if (null == lst || lst.isEmpty()) {
            return null;
        }

        String rtn = "";
        for (Integer l : lst) {
            rtn = rtn + COMMA + l.toString();
        }
        return isEmpty(rtn) ? rtn : rtn.substring(1);
    }

    /**
     * 字符串转LIST<Long>
     */
    public final static String ListLongToString(List<Long> lst) {
        if (null == lst || lst.isEmpty()) {
            return null;
        }

        String rtn = "";
        for (Long l : lst) {
            rtn = rtn + COMMA + l.toString();
        }
        return isEmpty(rtn) ? rtn : rtn.substring(1);
    }

    public final static String ListStringToString(List<String> lst) {
        if (null == lst || lst.isEmpty()) {
            return "''";
        }

        String rtn = "";
        for (String s : lst) {
            rtn = rtn + COMMA + SINGLE_QUOTATION + s + SINGLE_QUOTATION;
        }
        return isEmpty(rtn) ? rtn : rtn.substring(1);
    }

    /**
     * JSON格式List<String>转换
     *
     * @param jsonString
     * @return
     */
    public final static String JsonArrayStringToString(String jsonString) {
        String rtn;
        if (jsonString == null || isEmpty(jsonString)) {
            return null;
        }
        rtn = jsonString.replace("[", "").replace("]", "").replaceAll("\"", "");
        return rtn;
    }

    public static boolean isBlank(String str) {
        int length;
        if ((str == null) || ((length = str.length()) == 0)) {
            return true;
        }
        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    /**
     * 判断字符串是否为空(true:为空 false:不为空)
     *
     * @param inStr
     * @return
     */
    public final static boolean isEmpty(Object inStr) {
        if (inStr == null) {
            return true;
        }
        return EMPTY.equals(nullToEmpty(inStr));
    }

    /**
     * 判断字符串是否为数字(true:为空 false:不为空)
     *
     * @param inStr
     * @return
     */
    public final static boolean isNumeric(String inStr) {
        if (null == inStr || EMPTY.equals(inStr.trim())) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(inStr).matches();
    }

    /**
     * 字符串转化Integer类型
     *
     * @param inStr
     * @param def
     * @return
     */
    public final static Integer parseInteger(String inStr, Integer def) {
        try {
            return Integer.valueOf(inStr);
        } catch (Exception ex) {
            return def;
        }
    }

    /**
     * 字符串转化LONG类型
     *
     * @param inStr
     * @param def
     * @return
     */
    public final static Long parseLong(String inStr, Long def) {
        try {
            return Long.parseLong(inStr);
        } catch (Exception ex) {
            return def;
        }
    }

    public final static Long parseLong(Object inObjece, Long def) {
        try {
            return Long.parseLong(String.valueOf(inObjece));
        } catch (Exception ex) {
            return def;
        }
    }

    /**
     * 字符串转化Double类型
     *
     * @param inStr
     * @param def
     * @return
     */
    public final static Double parseDouble(String inStr, Double def) {
        try {
            return Double.parseDouble(inStr);
        } catch (Exception ex) {
            return def;
        }
    }

    /**
     * 转化字符为数字（不符合条件的返回默认值）
     *
     * @param obj
     * @param def
     * @return
     */
    public final static Integer parseInteger(Object obj, Integer def) {
        try {
            return Integer.valueOf(String.valueOf(obj));
        } catch (Exception ex) {
            return def;
        }
    }

    /**
     * 转化字符为布尔数学（不符合条件的返回默认值）
     *
     * @param obj
     * @param def
     * @return
     */
    public final static Boolean parseBoolean(Object obj, Boolean def) {
        if (obj == null) {
            return def;
        }

        try {
            return Boolean.valueOf(obj.toString());
        } catch (Exception ex) {
            return def;
        }
    }

    /**
     * 截取字符串,后面添加...
     *
     * @param name
     * @param maxLength
     * @return
     */
    public final static String cutString(String name, Integer maxLength) {
        if (maxLength == null) {
            maxLength = 20;
        }
        if (name == null || name.length() < 1) {
            return "";
        }

        Integer w       = 0;// 字符串长度，一个汉字长度为2
        Integer s       = 0;// 汉字个数
        boolean p       = false;// 判断字符串当前循环的前一个字符是否为汉字
        boolean b       = false;// 判断字符串当前循环的字符是否为汉字
        String  nameSub = "";

        for (int i = 0; i < name.length(); i++) {
            if (i > 1 && b == false) {
                p = false;
            }
            if (i > 1 && b == true) {
                p = true;
            }

            char c = name.charAt(i);
            // 单字节加1
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                w++;
                b = false;
            } else {
                w += 2;
                s++;
                b = true;
            }
            if (w > maxLength && i <= name.length() - 1) {
                if (b == true && p == true) {
                    nameSub = name.substring(0, i);
                }
                if (b == false && p == false) {
                    nameSub = name.substring(0, i - 1);
                }
                if (b == true && p == false) {
                    nameSub = name.substring(0, i);
                }
                if (p == true) {
                    nameSub = name.substring(0, i);
                }
                break;
            }
        }
        if (w <= maxLength) {
            return name;
        }
        return nameSub;
    }

    /**
     * 判断字符串中是否包含中文字符
     *
     * @param str
     * @return
     */
    public final static boolean containChineseWord(String str) {
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher matcher = pattern.matcher(str);

        return matcher.find();
    }

    /**
     * 去除空格 换行符等
     *
     * @param content
     * @return
     */
    public final static String replaceSpecialSymbols(String content) {
        return content.replaceAll("\n|\r|\t|&nbsp;", "");
    }

    /**
     * 截取字符串,后面添加...
     *
     * @param name
     * @param maxLength
     * @return
     */
    public final static String cutStringAddStr(String name, Integer maxLength) {
        if (maxLength == null) {
            maxLength = 20;
        }
        if (name == null || name.length() < 1) {
            return "";
        }

        Integer w       = 0;// 字符串长度，一个汉字长度为2
        Integer s       = 0;// 汉字个数
        boolean p       = false;// 判断字符串当前循环的前一个字符是否为汉字
        boolean b       = false;// 判断字符串当前循环的字符是否为汉字
        String  addStr  = "...";
        String  nameSub = "";

        for (int i = 0; i < name.length(); i++) {
            if (i > 1 && b == false) {
                p = false;
            }
            if (i > 1 && b == true) {
                p = true;
            }

            char c = name.charAt(i);
            // 单字节加1
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                w++;
                b = false;
            } else {
                w += 2;
                s++;
                b = true;
            }
            if (w > maxLength && i <= name.length() - 1) {
                if (b == true && p == true) {
                    nameSub = name.substring(0, i - 2) + addStr;
                }
                if (b == false && p == false) {
                    nameSub = name.substring(0, i - 3) + addStr;
                }
                if (b == true && p == false) {
                    nameSub = name.substring(0, i - 2) + addStr;
                }
                if (p == true) {
                    nameSub = name.substring(0, i - 2) + addStr;
                }
                break;
            }
        }
        if (w <= maxLength) {
            return name;
        }
        return nameSub;
    }

    /**
     * 中文转换
     *
     * @param str
     * @return
     */
    public final static String decode(String str) {
        try {
            byte[] unicode = str.getBytes(StandardCharsets.ISO_8859_1);
            return new String(unicode, StandardCharsets.UTF_8);
        } catch (Exception ex) {
            return str;
        }
    }

    /**
     * 字符转化为DATE格式(yyyy-MM-dd HH:mm:ss)
     *
     * @param value
     * @return
     */
    public final static Date parseDate(String value) {
        Date ret = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ret = sdf.parse(value);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }

    /**
     * native2ascii转码
     *
     * @param str
     * @return
     */
    public final static String native2ascii(String str) {
        if (str == null || isEmpty(str)) {
            return str;
        }
        String       tmp;
        StringBuffer sb = new StringBuffer(1000);
        char         c;
        int          i, j;
        sb.setLength(0);
        for (i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (c > 255) {
                sb.append("\\u");
                j = (c >>> 8);
                tmp = Integer.toHexString(j);
                if (tmp.length() == 1) {
                    sb.append("0");
                }
                sb.append(tmp);
                j = (c & 0xFF);
                tmp = Integer.toHexString(j);
                if (tmp.length() == 1) {
                    sb.append("0");
                }
                sb.append(tmp);
            } else {
                sb.append(c);
            }

        }
        return (new String(sb));
    }

    /**
     * @param str
     * @return
     */
    public final static String encodeUTF8(String str) {
        if (str == null || isEmpty(str)) {
            return str;
        }
        try {
            return URLEncoder.encode(str, "UTF-8").replace("+", " ");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 保留两位小数
     */
    public final static String sqrt(String str) {
        if (isEmpty(str)) {
            return str;
        }
        try {
            return df.format(Double.valueOf(str));
        } catch (Exception ex) {
            return str;
        }
    }

    /**
     * 判断日期类型
     *
     * @param value
     * @param format
     * @return
     */
    public final static Date parseData(String value, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(value);
        } catch (Exception ex) {
            return null;
        }
    }

    public final static Date parseData(Object value, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(value.toString());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 百分比
     *
     * @param dividend
     * @return
     */
    public final static String percent(Object dividend, Object divisor) {
        try {
            double dividend_d = parseDouble(dividend.toString(), 0d);
            double divisor_d  = parseDouble(divisor.toString(), 0d);
            if (dividend_d <= 0 || divisor_d <= 0) {
                return "0.00";
            }
            Double        d             = dividend_d * 100.00d / divisor_d;
            DecimalFormat decimalFormat = new DecimalFormat("##.##");
            decimalFormat.setMinimumFractionDigits(2);
            String rtn = decimalFormat.format(d).replace(".00", "");
            return rtn;
        } catch (Exception ex) {
            return "0.00";
        }
    }

    /**
     * 价格格式化
     *
     * @param dividend
     * @return
     */
    public final static String price(Object dividend) {
        try {
            double        dividend_d    = parseDouble(dividend.toString(), 0d);
            Double        d             = dividend_d / 100.00d;
            DecimalFormat decimalFormat = new DecimalFormat("##.##");
            decimalFormat.setMinimumFractionDigits(2);
            String rtn = decimalFormat.format(d).replace(".00", "");
            return rtn;
        } catch (Exception ex) {
            return "0";
        }
    }

    public final static String rateFee(Object dividend) {
        try {
            double dividend_d = parseDouble(dividend.toString(), 0d);
            if (dividend_d <= 0) {
                return "0.01";
            }
            Double d = dividend_d / 100.00d;
            if (d <= 0) {
                return "0.01";
            }
            String[] array = String.valueOf(d).split("\\.");
            if (array.length != 2) {
                return price(dividend);
            }

            StringBuffer rtn = new StringBuffer();
            rtn.append(array[0]);
            switch (array[1].length()) {
                case 1:
                    if (!"0".equals(array[1])) {
                        rtn.append(".");
                        rtn.append(array[1]);
                    }
                    break;
                case 2:
                    if (!"00".equals(array[1])) {
                        rtn.append(".");
                        rtn.append(array[1]);
                    }
                    break;
                default:
                    if ("00".equals(array[1].substring(0, 2))) {
                        rtn.append(".01");
                    } else {
                        rtn.append(".");
                        rtn.append(array[1], 0, 2);
                    }
                    break;
            }
            return rtn.toString();
        } catch (Exception ex) {
            return "0.01";
        }
    }

    /**
     * 匹配内容信息
     *
     * @param value
     * @param rs
     * @param re
     * @return
     */
    public static String matchString(String value, String rs, String re) {
        try {
            String temp  = null;
            int    start = value.indexOf(rs);
            if (start >= 0) {
                temp = value.substring(start);
            }
            int end = temp.indexOf(re);
            return temp.substring(rs.length(), end);
        } catch (Exception ex) {
            return null;
        }
    }

    private static final byte UPPER_A  = 'A';
    private static final byte UPPER_Z  = 'Z';
    private static final byte LOW_A    = 'a';
    private static final byte LOW_Z    = 'z';
    private static final byte TO_UPPER = LOW_A - UPPER_A;

    /**
     * 由纯大写字母组成
     *
     * @param data
     * @return
     */
    public static boolean isUpper(String data) {
        for (char item : data.toCharArray()) {
            if (!(item >= UPPER_A && item <= UPPER_Z)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 匈牙利命名法转驼峰命名法
     *
     * @param stringValue
     * @return
     */
    public static String changeHungaryToCamel(String stringValue) {
        if (stringValue.indexOf("_") >= 0 || isUpper(stringValue)) {
            stringValue = stringValue.toLowerCase();
            int    _index    = 0;
            int    valLength = stringValue.length();
            String oldChar;
            String newChar;
            while ((_index = stringValue.indexOf('_')) >= 0) {
                if (_index + 1 < valLength && (stringValue.charAt(_index + 1) >= LOW_A && stringValue.charAt(_index + 1) <= LOW_Z)) {
                    oldChar = new String(new char[]{'_', stringValue.charAt(_index + 1)});
                    newChar = String.valueOf((char) (stringValue.charAt(_index + 1) - TO_UPPER));
                } else {
                    oldChar = String.valueOf('_');
                    newChar = "";
                }
                stringValue = stringValue.replace(oldChar, newChar);
            }
        }
        return stringValue;
    }

    /**
     * 驼峰转匈牙利命名法
     *
     * @param stringValue
     * @return
     */
    public static String changeCamelToHungary(String stringValue) {
        if (!(stringValue.indexOf("_") >= 0)) {
            int    _index = 0;
            String oldChar;
            String newChar;
            while ((_index = indexOfUpper(stringValue)) >= 0) {
                oldChar = String.valueOf(stringValue.charAt(_index));
                newChar = new String(new char[]{'_', (char) (stringValue.charAt(_index) + TO_UPPER)});
                stringValue = stringValue.replace(oldChar, newChar);
            }
        }
        return stringValue.toUpperCase();
    }

    /**
     * 返回大写字母的位置
     *
     * @param stringValue
     * @return
     */
    private static int indexOfUpper(String stringValue) {
        char[] cs = stringValue.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            if (cs[i] >= UPPER_A && cs[i] <= UPPER_Z) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 判断字符串非空
     *
     * @param data
     * @return
     */
    public static boolean notEmpty(String data) {
        return !isEmpty(data);
    }

    /**
     * 将MAP 的KEY 由匈牙利命名法改成驼峰命名法
     *
     * @param groupDate
     * @return
     */
    public static Map<String, String> changeHungaryToCamel(Map<String, String> groupDate) {
        Map<String, String> newDate = new LinkedHashMap<String, String>();
        for (Map.Entry<String, String> item : groupDate.entrySet()) {
            newDate.put(changeHungaryToCamel(item.getKey()), item.getValue());
        }
        groupDate.clear();
        return newDate;
    }

    /**
     * 在字符串的最后方加小数点<br>
     * 如： addLastDot("abc",2) 返回 a.bc<br>
     * 如果位数不足，在前方补0 如： addLastDot("a",2) 返回 0.0a
     *
     * @param string
     * @param lastIndex
     * @return
     * @Title: addLastDot
     * @Description: TODO
     */
    public static String addLastDot(String string, int lastIndex) {
        StringBuilder builder = new StringBuilder(string);
        boolean       isNeg   = false;
        if (builder.charAt(0) == '-') {
            builder = builder.delete(0, 1);
            isNeg = true;
        }
        int length = builder.length();
        for (int i = 0; i <= lastIndex - length; i++) {
            builder.insert(0, '0');
        }
        builder.insert(builder.length() - lastIndex, '.');
        if (isNeg) {
            builder.insert(0, '-');
        }
        return builder.toString();
    }

    /**
     * 在字符串后添加
     *
     * @param string
     * @param appendChar
     * @param length
     * @return
     * @Title: append
     * @Description: TODO
     */
    public static String append(String string, char appendChar, int length) {
        char[] c = new char[length];
        Arrays.fill(c, appendChar);
        string = string == null ? "" : string;
        StringBuilder builder = new StringBuilder(string.length() + length + 1);
        return builder.append(string).append(c).toString();
    }

    /**
     * 将字符串最后的 length位转换成 coverChar
     *
     * @param string
     * @param coverChar
     * @param length
     * @return
     * @Title: coverLastChar
     * @Description: TODO
     */
    public static String coverLastChar(String string, char coverChar, int length) {
        if (string != null && string.length() >= length) {
            char[] c = new char[length];
            Arrays.fill(c, coverChar);
            StringBuilder builder = new StringBuilder(string);
            return builder.replace(builder.length() - length, builder.length(), new String(c)).toString();
        }
        return string;
    }

    /**
     * 字符串从头替换
     *
     * @param string
     * @param cover
     * @return
     */
    public static String coverFirst(String string, String cover) {
        if (string != null && string.length() >= cover.length()) {
            StringBuilder builder = new StringBuilder(string);
            return builder.replace(0, cover.length(), cover).toString();
        }
        return string;
    }

    /**
     * 字符串从头替换
     *
     * @param string
     * @param cover
     * @return
     */
    public static String coverFirst(String string, char cover) {
        if (string != null && string.length() >= 1) {
            StringBuilder builder = new StringBuilder(string);
            builder.setCharAt(0, cover);
            return builder.toString();
        }
        return string;
    }

    /**
     * 字符串从头删除
     *
     * @param string 字符串
     * @param length 删除长度
     * @return
     */
    public static String deleteFirst(String string, int length) {
        if (string != null && string.length() >= length) {
            StringBuilder builder = new StringBuilder(string);
            return builder.delete(0, length).toString();
        }
        return string;
    }

    public static char toUpper(char c) {
        if (c >= LOW_A && c <= LOW_Z) {
            return (char) (c - TO_UPPER);
        }
        return c;
    }

    public static char toLower(char c) {
        if (c >= UPPER_A && c <= UPPER_Z) {
            return (char) (c + TO_UPPER);
        }
        return c;
    }

    public static String insert(String string, char insertVal, int length) {
        char[] c = new char[length];
        Arrays.fill(c, insertVal);
        if (string != null) {
            return new StringBuilder(string.length() + length).append(c).append(string).toString();
        }
        return null;
    }

    static byte ZERO  = '0';
    static byte NIGHT = '9';

    /**
     * 判断参数是一个 Integer 的数字
     *
     * @param innerItem
     * @return
     * @Title: isInteger
     * @Description: TODO
     */
    public static boolean isInteger(String innerItem) {
        if (isEmpty(innerItem)) {
            return false;
        }
        char[] cs = innerItem.toCharArray();
        int    i  = 0;
        if (cs[0] == '-' && cs.length >= 2) {
            i = 1;
        }
        char c;
        for (; i < cs.length; i++) {
            c = cs[i];
            if (!(c >= ZERO && c <= NIGHT)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断参数是一个  浮点类型数字
     *
     * @param innerItem
     * @return
     * @Title: isInteger
     * @Description: TODO
     */
    public static boolean isDouble(String innerItem) {
        if (innerItem == null) {
            return false;
        }
        boolean hasDot = false;
        char[]  cs     = innerItem.toCharArray();

        int i = 0;
        if (cs[0] == '-' && cs.length >= 2) {
            i = 1;
        }
        char c;
        for (; i < cs.length; i++) {
            c = cs[i];
            if (!(c >= ZERO && c <= NIGHT)) {
                if (!hasDot && c == '.') {
                    hasDot = true;
                    continue;
                }
                return false;
            }
        }
        return true;
    }

    /**
     * 判断参数是一个  浮点类型数字 保留N位小数
     *
     * @param innerItem
     * @param n         N位小数
     * @return
     * @Title: isInteger
     * @Description: TODO
     */
    public static boolean isDouble(String innerItem, int n) {
        if (innerItem == null) {
            return false;
        }
        boolean hasDot = false;
        char[]  cs     = innerItem.toCharArray();

        int i = 0;
        if (cs[0] == '-' && cs.length >= 2) {
            i = 1;
        }
        char c;

        for (; i < cs.length; i++) {
            c = cs[i];
            if (!(c >= ZERO && c <= NIGHT)) {
                if (!hasDot && c == '.') {
                    hasDot = true;
                    continue;
                }

                return false;
            }
        }
        return true;
    }

    public static String format(String msg, String key, Object value) {
        return msg.replaceAll("\\{" + key + "\\}", String.valueOf(value == null ? "" : value));
    }

    public static String removePlaceholder(String msg) {
        return msg.replaceAll("\\{(.*?)\\}", "");
    }

    /**
     * 从左边增加指定字符
     *
     * @param c       被指定的字符
     * @param length  内容长度
     * @param content 内容
     * @return
     * @Title: appendLeft
     * @Description: TODO
     */
    public static String appendLeft(char c, int length, Object content) {
        String result = String.valueOf(content);
        length = length - result.length();
        char[] cs = null;
        if (length > 0) {
            cs = new char[length];
            Arrays.fill(cs, c);
            return new StringBuilder(length + result.length()).append(cs).append(result).toString();
        }
        return result;
    }

    public static Map<String, String> toQueryMap(String paramStr) {
        String[]            vals      = split(paramStr, "&");
        Map<String, String> resultMap = new HashMap<String, String>(vals.length);
        int                 index;
        for (int i = 0; i < vals.length; i++) {
            index = vals[i].indexOf("=");
            resultMap.put(vals[i].substring(0, index), vals[i].substring(index + 1));
        }
        return resultMap;
    }

    public static String[] split(String msg, String cut) {
        int          index   = 0;
        int          befault = 0;
        int          i       = 0;
        List<String> l       = new ArrayList<String>(128);
        do {
            index = msg.indexOf(cut, index);
            l.add(msg.substring(befault, index == -1 ? msg.length() : index));
            i++;
            index += 1;
            befault = index + cut.length() - 1;
        } while (index > 0 && index < msg.length());
        return l.toArray(new String[l.size()]);
    }

    public static String nullToEmpty(Object value) {
        if (value == null) {
            return EMPTY;
        } else {
            return value.toString().trim();
        }
    }

    /**
     * UNICODE字符转换
     *
     * @param theString
     * @return
     */
    public static String decodeUnicode(String theString) {
        if (theString.contains("&#x")) {
            theString = theString.replaceAll("&#x", "\\\\u").replaceAll(";", "");
        }

        char         aChar;
        int          len       = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);

        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't') {
                        aChar = '\t';
                    } else if (aChar == 'r') {
                        aChar = '\r';
                    } else if (aChar == 'n') {
                        aChar = '\n';
                    } else if (aChar == 'f') {
                        aChar = '\f';
                    }
                    outBuffer.append(aChar);
                }
            } else {
                outBuffer.append(aChar);
            }
        }
        return outBuffer.toString();
    }

    public static String UrlDecode(String value) {
        try {
            return URLDecoder.decode(value, "utf-8");
        } catch (Exception ex) {
            return value;
        }
    }

    public static String UrlEncode(String value) {
        try {
            return URLEncoder.encode(value, "utf-8");
        } catch (Exception ex) {
            return value;
        }
    }

    /**
     * 将一个数字处理为以万为单位的字符串，保留两位小数
     *
     * @param num
     * @return
     */
    public static String getTenThousandOfANumber(Integer num) {
        if (num < 10000) {
            return String.valueOf(num);
        }
        String   numStr = new DecimalFormat("#.00").format(num / 10000d);
        String[] ss     = numStr.split("\\.");
        if ("00".equals(ss[1])) {
            return ss[0] + "W";
        } else if ('0' == (ss[1].charAt(1))) {
            return ss[0] + "." + ss[1].charAt(0) + "W";
        } else {
            return numStr + "W";
        }
    }

    public static String getFileName(String realPath) {
        String   fileName = null;
        String[] splitStr = realPath.split("/");
        fileName = splitStr[splitStr.length - 1];
        return fileName;
    }

    public static String jsonKey(String json, String key) {
        try {
            JSONObject jsonObject = JSON.parseObject(json);
            return nullToEmpty(jsonObject.getString(key));
        } catch (Exception ex) {
            return EMPTY;
        }
    }

    /**
     * 将将输入字符串通过小写的第一个字符的大小写匹配
     *
     * @param inputString             the input string
     * @param firstCharacterUppercase the first character uppercase
     * @return the camel case string
     */
    public static String getCamelCaseString(String inputString, boolean firstCharacterUppercase) {
        StringBuffer sb            = new StringBuffer();
        boolean      nextUpperCase = false;
        for (int i = 0; i < inputString.length(); i++) {
            char c = inputString.charAt(i);
            switch (c) {
                case '_':
                case '-':
                case '@':
                case '$':
                case '#':
                case '/':
                case ' ':
                    if (sb.length() > 0) {
                        nextUpperCase = true;
                    }
                    break;
                default:
                    if (nextUpperCase) {
                        sb.append(Character.toUpperCase(c));
                        nextUpperCase = false;
                    } else {
                        sb.append(Character.toLowerCase(c));
                    }
                    break;
            }
        }
        if (firstCharacterUppercase) {
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        }
        return sb.toString();
    }

    public static String hex(String strString) {
        if (isEmpty(strString)) {
            return "";
        } else {
            byte[] bytes = strString.getBytes();
            char[] chars = new char[bytes.length * 2];

            for (int i = 0; i < bytes.length; ++i) {
                byte b = bytes[i];
                chars[i * 2] = hexDigits[(b & 240) >> 4];
                chars[i * 2 + 1] = hexDigits[b & 15];
            }

            return new String(chars);
        }
    }

    public static String xeh(String str) {
        if (isEmpty(str)) {
            return "";
        } else {
            int    length = str.length() / 2;
            byte[] bytes  = new byte[length];
            byte[] source = str.getBytes();

            for (int i = 0; i < bytes.length; ++i) {
                byte bh = Byte.decode("0x" + new String(new byte[]{source[i * 2]})).byteValue();
                bh = (byte) (bh << 4);
                byte bl = Byte.decode("0x" + new String(new byte[]{source[i * 2 + 1]})).byteValue();
                bytes[i] = (byte) (bh ^ bl);
            }

            return new String(bytes);
        }
    }
}
